package com.yy.permissions_general.common.preview.aspect;


import com.yy.permissions_general.common.constant.StatusEnum;
import com.yy.permissions_general.common.config.exception.base.ServiceException;
import com.yy.permissions_general.utils.ServletUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
* @Description: 演示环境拦截器
* @Author: YY
* @Date: 2021/12/19
*/
@Slf4j
@Aspect
@Component
public class PreviewAspect {

    @Value("${custom.yy.isPreview}")
    private boolean isPreview = false;

    @Pointcut("execution(* com.yy.permissions_general.module.*.controller.*.*(..)) " +
            "&& !execution(* com.yy.permissions_general.module.*.controller.*.select*(..)) " +
            "&& !execution(* com.yy.permissions_general.module.*.controller.*.list*(..))" +
            "&& execution(* com.yy.permissions_general.module.*.*.controller.*.*(..)) " +
            "&& !execution(* com.yy.permissions_general.module.*.*.controller.*.select*(..)) " +
            "&& !execution(* com.yy.permissions_general.module.*.*.controller.*.list*(..))" +
            "&& !execution(* com.yy.permissions_general.module.system.user.controller.CaptchaController.captcha(..)) ")
    public void controllerAspect() {
    }

    @Around("controllerAspect()")
    public Object aroundApi(ProceedingJoinPoint point) throws Throwable {
        HttpServletRequest request = ServletUtil.getRequest();
        if (StringUtils.equalsIgnoreCase(request.getMethod(), HttpMethod.POST.name()) && isPreview) {
            log.error("演示环境不能操作~");
            throw new ServiceException(StatusEnum.PREVIEW_EXCEPTION);
        }
        return point.proceed();
    }

}
